//
//  LMInfiniteHUDView.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THInfiniteHUDView.h"
#import "THInfiniteAnimateView.h"
@interface THInfiniteHUDView ()
@property(nonatomic,retain) UIView *backgroundView;
@property(nonatomic,retain) THInfiniteAnimateView *centerAnimatedView;
@property(nonatomic,retain) THInfiniteAnimateView *leftAnimatedView;
@property(nonatomic,retain) THInfiniteAnimateView *rightAnimatedView;
@property(nonatomic,retain) NSTimer *showTimer;//timer
@property (nonatomic, strong) NSArray *paddingConstraints;
@end
@implementation THInfiniteHUDView

//解决当父View释放时，当前视图因为被Timer强引用而不能释放的问题
- (void)willMoveToSuperview:(UIView *)newSuperview{
    if (!newSuperview) {
        [self invailTimer];
    }else{
        
    }
}
+ (instancetype)showHUDAddedTo:(UIView *)view {
    THInfiniteHUDView *HUDView = [[self alloc] initWithView:view];
    [view addSubview:HUDView];
    return HUDView;
}
- (instancetype)initWithView:(UIView*)view{
    NSAssert(view, @"View must not be nil.");
    return [self initWithFrame:view.bounds];
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self commonInit];
    }
    return self;
}
- (void)commonInit{
    
    _fillColor    = [UIColor colorWithRed:255/255.0 green:216/255.0 blue:64/255.0 alpha:1.0];
    _cornerRadius = 4.0f;
    _margin       = 2.0;
    _offsetY      = 18.0;
    self.opaque = NO;
    self.backgroundColor = [UIColor clearColor];
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    // Make it invisible for now
    self.alpha = 0.0f;
    self.layer.allowsGroupOpacity = NO;
    [self setupUI];
}
- (void)setupUI{
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:self.bounds];
    backgroundView.alpha = 1.0f;
    backgroundView.backgroundColor = [UIColor clearColor];
    backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self addSubview:backgroundView];
    _backgroundView = backgroundView;
    THInfiniteAnimateView *centerAnimatedView = [THInfiniteHUDView createAnimateViewColor:self.fillColor radius:self.cornerRadius inView:backgroundView];
    THInfiniteAnimateView *leftAnimatedView   = [THInfiniteHUDView createAnimateViewColor:self.fillColor radius:self.cornerRadius inView:backgroundView];
    THInfiniteAnimateView *rightAnimateView   = [THInfiniteHUDView createAnimateViewColor:self.fillColor radius:self.cornerRadius inView:backgroundView];

    self.centerAnimatedView = centerAnimatedView;
    self.leftAnimatedView   = leftAnimatedView;
    self.rightAnimatedView  = rightAnimateView;
    [self updateConstraints];
}
- (void)updateConstraints{
    
    CGFloat animatedViewWidth = CGRectGetWidth(self.centerAnimatedView.frame);
    [self removeConstraints:self.constraints];
    NSLayoutConstraint *centerXLayoutView = [NSLayoutConstraint constraintWithItem:self.centerAnimatedView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.backgroundView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    NSLayoutConstraint *centerYLayoutView = [NSLayoutConstraint constraintWithItem:self.centerAnimatedView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.backgroundView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
    
    NSLayoutConstraint *leftXLayoutView = [NSLayoutConstraint constraintWithItem:self.leftAnimatedView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.backgroundView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:-self.margin- animatedViewWidth];
    NSLayoutConstraint *leftYLayoutView = [NSLayoutConstraint constraintWithItem:self.leftAnimatedView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.backgroundView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
    
    NSLayoutConstraint *rightXLayoutView = [NSLayoutConstraint constraintWithItem:self.rightAnimatedView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.backgroundView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:self.margin +animatedViewWidth];
    NSLayoutConstraint *rightYLayoutView = [NSLayoutConstraint constraintWithItem:self.rightAnimatedView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.backgroundView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
    
    [self addConstraint:centerXLayoutView];
    [self addConstraint:centerYLayoutView];
    [self addConstraint:leftXLayoutView];
    [self addConstraint:leftYLayoutView];
    [self addConstraint:rightXLayoutView];
    [self addConstraint:rightYLayoutView];
    [super updateConstraints];
}
- (void)showInfiniteAnimatedView{
    
    [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.leftAnimatedView.transform = CGAffineTransformTranslate(self.leftAnimatedView.transform, 0,-self.offsetY);
        self.leftAnimatedView.alpha   = 0.2;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.centerAnimatedView.transform = CGAffineTransformTranslate(self.centerAnimatedView.transform, 0,-self.offsetY);
             self.leftAnimatedView.transform  = CGAffineTransformIdentity;
            self.leftAnimatedView.alpha     = 1.0;
            self.centerAnimatedView.alpha   = 0.2;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                self.rightAnimatedView.transform  = CGAffineTransformTranslate(self.rightAnimatedView.transform, 0,-self.offsetY);
                self.centerAnimatedView.transform = CGAffineTransformIdentity;
                self.centerAnimatedView.alpha = 1.0;
                self.rightAnimatedView.alpha  = 0.2;
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                    self.rightAnimatedView.transform  =  CGAffineTransformIdentity;
                    self.rightAnimatedView.alpha      = 1.0;
                } completion:^(BOOL finished) {
                   
                }];
            }];
        }];
    }];
}
+ (THInfiniteAnimateView*)createAnimateViewColor:(UIColor*)fillColor
                                         radius:(CGFloat)radius
                                         inView:(UIView*)inView{
    THInfiniteAnimateView *animatedView = [[THInfiniteAnimateView alloc] initWithFrame:CGRectZero];
    animatedView.strokeColor = fillColor;
    animatedView.strokeThickness = 0.0;
    animatedView.radius = radius;
    animatedView.translatesAutoresizingMaskIntoConstraints = NO;
    [animatedView sizeToFit];
    
    [inView addSubview:animatedView];
    return animatedView;
}
- (void)showHUDView{
    self.alpha = 1.0;
    if (self.showTimer) {
        [self invailTimer];
    }
    self.showTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showInfiniteAnimatedView) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.showTimer forMode:NSRunLoopCommonModes];
    [self.showTimer fire];
}
- (void)hideHUDView{
    [self invailTimer];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showInfiniteAnimatedView) object:nil];
    [self removeFromSuperview];
}
- (void)invailTimer{
    if (_showTimer) {
        [_showTimer invalidate];
        _showTimer = nil;
    }
}
@end
